<template>
    <div>
        <a-table :dataSource="taskList"
                 :columns="columns"
                 :pagination="pagination"
                 @change="handlerChange"
                 style="margin-top: 10px"
                 bordered>
            <template #createTime="{text,record}">
                {{this.$moment(text).format('YYYY-MM-DD HH:mm:ss')}}
            </template>
            <template v-slot:action="{text,record}">
                <a-space>
                    <a-button type="primary" @click="showDrawer(record)">办理任务</a-button>
                    <a-popconfirm
                            title="删除数据无法恢复，请确认是否删除？"
                            ok-text="是"
                            cancel-text="否"
                            @confirm="okDelete(record.id)"
                    >
                        <a-button type="danger">删除</a-button>
                    </a-popconfirm>
                </a-space>
            </template>
        </a-table>
        <a-drawer
                title="请假申请单"
                :width="1000"
                :visible="leaveVisible"
                :body-style="{ paddingBottom: '40px' }"
                :footer-style="{ textAlign: 'right' }"
                @close="onClose"
        >
            <a-form :model="drawerData.leaveBill" layout="vertical">
                <a-row :gutter="16">
                    <a-col :span="12">
                        <a-form-item label="标题" name="title">
                            <a-input v-model:value="drawerData.leaveBill.title" :disabled="true"/>
                        </a-form-item>
                    </a-col>
                    <a-col :span="6">
                        <a-form-item label="请假天数" name="day">
                            <a-input v-model:value="drawerData.leaveBill.day" :disabled="true"/>
                        </a-form-item>
                    </a-col>
                    <a-col :span="6">
                        <a-form-item label="状态" name="state">
                            <a-tag v-if="drawerData.leaveBill.userId == loginUser.id
                            && drawerData.comments.length != 0" color="warning">
                                <template #icon>
                                    <exclamation-circle-outlined/>
                                </template>
                                被驳回
                            </a-tag>
                            <a-tag v-else color="processing">
                                <template #icon>
                                    <sync-outlined :spin="true"/>
                                </template>
                                审批中
                            </a-tag>

                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-form-item label="请假原因" name="remark">
                            <a-textarea v-model:value="drawerData.leaveBill.remark"
                                        :disabled="true" :rows="3"/>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-form-item label="批注"
                                     v-bind="validateInfos.comment"
                                     name="comment">
                            <a-textarea v-model:value="drawerData.comment" :rows="3"/>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-space>
                            <a-button v-for="(item,index) in drawerData.buttonList"
                                      :key="index" type="primary"
                                      @click="submitOk(item)">
                                {{item}}
                            </a-button>
                        </a-space>
                    </a-col>
                </a-row>
            </a-form>
            <a-table :dataSource="drawerData.comments"
                     :columns="commentColumns"
                     :pagination="false"
                     @change="handlerChange"
                     bordered>
                <template #user="{text,record}">
                    {{beLongUser(text)}}
                </template>
                <template #time="{text,record}">
                    {{this.$moment(text).format('YYYY-MM-DD HH:mm:ss')}}
                </template>
            </a-table>
        </a-drawer>
        <a-drawer
                title="出库申请单"
                :width="1000"
                :visible="outBoundVisible"
                :body-style="{ paddingBottom: '40px' }"
                :footer-style="{ textAlign: 'right' }"
                @close="onClose"
        >
            <a-form :model="drawerData.outBoundBill" layout="vertical">
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-form-item label="标题" name="title">
                            <a-input v-model:value="drawerData.outBoundBill.title" :disabled="true"/>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="12">
                        <a-form-item label="出库商品" name="goodsId">
                            <a-input :value="beLongGoods(drawerData.outBoundBill.goodsId)" :disabled="true"/>
                        </a-form-item>
                    </a-col>
                    <a-col :span="6">
                        <a-form-item label="出库数量" name="quantity">
                            <a-input v-model:value="drawerData.outBoundBill.quantity" :disabled="true"/>
                        </a-form-item>
                    </a-col>
                    <a-col :span="6">
                        <a-form-item label="状态" name="state">
                            <a-tag v-if="drawerData.outBoundBill.userId == loginUser.id
                            && drawerData.comments.length != 0" color="warning">
                                <template #icon>
                                    <exclamation-circle-outlined/>
                                </template>
                                被驳回
                            </a-tag>
                            <a-tag v-else color="processing">
                                <template #icon>
                                    <sync-outlined :spin="true"/>
                                </template>
                                审批中
                            </a-tag>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-form-item label="出库详情" name="remark">
                            <a-textarea v-model:value="drawerData.outBoundBill.remark"
                                        :disabled="true" :rows="3"/>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-form-item label="批注" name="comment">
                            <a-textarea v-model:value="drawerData.comment" :rows="3"/>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-space>
                            <a-button v-for="(item,index) in drawerData.buttonList"
                                      :key="index" type="primary"
                                      @click="submitOk(item)">
                                {{item}}
                            </a-button>
                        </a-space>
                    </a-col>
                </a-row>
            </a-form>
            <a-table :dataSource="drawerData.comments"
                     :columns="commentColumns"
                     :pagination="false"
                     bordered>
                <template #time="{text,record}">
                    {{this.$moment(text).format('YYYY-MM-DD HH:mm:ss')}}
                </template>
                <template #user="{text,record}">
                    {{beLongUser(text)}}
                </template>
            </a-table>
        </a-drawer>
        <a-drawer
                title="采购申请单"
                :width="1000"
                :visible="procurementVisible"
                :body-style="{ paddingBottom: '40px' }"
                :footer-style="{ textAlign: 'right' }"
                @close="onClose"
        >
            <a-form :model="drawerData.outBoundBill" layout="vertical">
                <a-row :gutter="16">
                    <a-col :span="12">
                        <a-form-item label="标题" name="title">
                            <a-input v-model:value="drawerData.procurementBill.title" :disabled="true"/>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="合作公司" name="companyId">
                            <a-input :value="beLongCompany(drawerData.procurementBill.companyId)" :disabled="true"/>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="12">
                        <a-form-item label="采购旗下商品" name="goodsId">
                            <a-input :value="beLongGoods(drawerData.procurementBill.goodsId)" :disabled="true"/>
                        </a-form-item>
                    </a-col>
                    <a-col :span="6">
                        <a-form-item label="采购数量" name="quantity">
                            <a-input v-model:value="drawerData.procurementBill.quantity" :disabled="true"/>
                        </a-form-item>
                    </a-col>
                    <a-col :span="6">
                        <a-form-item label="状态" name="state">
                            <a-tag v-if="drawerData.procurementBill.userId == loginUser.id
                            && drawerData.comments.length != 0" color="warning">
                                <template #icon>
                                    <exclamation-circle-outlined/>
                                </template>
                                被驳回
                            </a-tag>
                            <a-tag v-else color="processing">
                                <template #icon>
                                    <sync-outlined :spin="true"/>
                                </template>
                                审批中
                            </a-tag>

                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-form-item label="采购描述" name="remark">
                            <a-textarea v-model:value="drawerData.procurementBill.remark"
                                        :disabled="true" :rows="3"/>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-form-item label="批注" name="comment">
                            <a-textarea v-model:value="drawerData.comment" :rows="3"/>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-space>
                            <a-button v-for="(item,index) in drawerData.buttonList"
                                      :key="index" type="primary"
                                      @click="submitOk(item)">
                                {{item}}
                            </a-button>
                        </a-space>
                    </a-col>
                </a-row>
            </a-form>
            <a-table :dataSource="drawerData.comments"
                     :columns="commentColumns"
                     :pagination="false"
                     bordered>
                <template #time="{text,record}">
                    {{this.$moment(text).format('YYYY-MM-DD HH:mm:ss')}}
                </template>
                <template #user="{text,record}">
                    {{beLongUser(text)}}
                </template>
            </a-table>
        </a-drawer>
    </div>
</template>

<script>
    import {ref, onMounted} from 'vue'
    import axios from 'axios'
    import {message, Form} from 'ant-design-vue'
    import {SyncOutlined, ExclamationCircleOutlined} from '@ant-design/icons-vue'
    import {SessionStorage} from '../../../public/session-storage'

    export default {
        name: "myself-toDoItems",
        components: {
            SyncOutlined,
            ExclamationCircleOutlined
        },
        setup() {
            const columns = [
                {
                    title: '任务ID',
                    dataIndex: 'id',
                    key: 'id',
                    align: 'center'
                },
                {
                    title: '任务名称',
                    dataIndex: 'name',
                    key: 'name',
                    align: 'center'
                },
                {
                    title: '申请时间',
                    dataIndex: 'createTime',
                    key: 'createTime',
                    slots: {customRender: 'createTime'},
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    slots: {customRender: 'action'},
                    align: 'center'
                }
            ]
            const commentColumns = [
                {
                    title: '时间',
                    dataIndex: 'time',
                    key: 'time',
                    slots: {customRender: 'time'}
                },
                {
                    title: '批注人',
                    dataIndex: 'userId',
                    key: 'userId',
                    slots: {customRender: 'user'}
                },
                {
                    title: '批注信息',
                    dataIndex: 'fullMessage',
                    key: 'fullMessage',
                },
            ]

            const useForm = Form.useForm
            const loginUser = ref({})
            const task = ref({})
            const taskList = ref()
            const userList = ref()
            const goodsList = ref()
            const companyList = ref()
            const drawerData = ref({
                comment: ''
            })
            const {validate, validateInfos} = useForm(drawerData, {
                comment: [{required: true, message: '请输入批注信息', trigger: ['submit', 'change']}]
            })
            const pagination = ref({
                current: 1,
                pageSize: 6,
            })
            const leaveVisible = ref(false)
            const procurementVisible = ref(false)
            const outBoundVisible = ref(false)

            const load = () => {
                axios.get('/task/queryByUserId/' + loginUser.value.id, {
                    params: {
                        pageNum: pagination.value.current,
                        pageSize: pagination.value.pageSize,
                    }
                }).then(rs => {
                    console.log("查找待办事务结果：", rs)
                    taskList.value = rs.data.data.list
                    pagination.value.total = rs.data.data.total
                })
            }

            const handlerChange = (pageInfo) => {
                pagination.value = pageInfo
                load()
            }

            const okDelete = (id) => {
                console.log("要删除的ID：" + id)
                axios.delete('/task/delete/' + id).then(rs => {
                    if (rs.data.code == 200) {
                        message.success("删除成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const showDrawer = obj => {
                task.value = JSON.parse(JSON.stringify(obj))
                axios.get('/task/viewTaskForm/' + obj.id).then(rs => {
                    console.log("获取task信息结果：", rs)
                    drawerData.value = rs.data.data
                    drawerData.value.comment = ''
                    if (drawerData.value.applyType == 'leave') {
                        leaveVisible.value = true
                    } else if (drawerData.value.applyType == 'procurement') {
                        procurementVisible.value = true
                    } else if (drawerData.value.applyType == 'outBound') {
                        outBoundVisible.value = true
                    }
                })
            }
            const onClose = () => {
                console.log("关闭")
                // leaveVisible.value = false
                leaveVisible.value = procurementVisible.value = outBoundVisible.value = false
            }
            const submitOk = (buttonValue) => {
                // message.success("提交")
                validate().then(() => {
                    axios.post('/task/complete', {
                        buttonValue: buttonValue,
                        userId: loginUser.value.id,
                        taskId: task.value.id,
                        comment: drawerData.value.comment
                    }).then(rs => {
                        if (rs.data.code == 200) {
                            message.success("审批成功")
                            leaveVisible.value = procurementVisible.value = outBoundVisible.value = false
                            load()
                        }
                    })
                }).catch(() => {
                    console.log("校验不通过")
                })
            }

            const beLongUser = (userId) => {
                let rs = ''
                userList.value.forEach(val => {
                    if (val.id == userId) {
                        rs = val.username
                        return rs
                    }
                })
                return rs == '' ? userId : rs
            }
            const beLongGoods = (goodsId) => {
                let rs = ''
                goodsList.value.forEach(val => {
                    if (val.id == goodsId) {
                        rs = val.name
                        return rs
                    }
                })
                return rs == '' ? goodsId : rs
            }
            const beLongCompany = (companyId) => {
                let rs = ''
                companyList.value.forEach(val => {
                    if (val.id == companyId) {
                        rs = val.name
                        return rs
                    }
                })
                return rs == '' ? companyId : rs
            }

            onMounted(() => {
                loginUser.value = SessionStorage.get('loginUser')
                axios.get('/user/queryAll').then(rs => {
                    userList.value = rs.data.data
                })
                axios.get('goods/queryAll').then(rs => {
                    goodsList.value = rs.data.data
                })
                axios.get('company/queryAll').then(rs => {
                    companyList.value = rs.data.data
                })
                load()
            })
            return {
                columns,
                pagination,
                handlerChange,
                load,
                okDelete,
                task,
                taskList,
                showDrawer,
                leaveVisible,
                procurementVisible,
                outBoundVisible,
                onClose,
                submitOk,
                drawerData,
                commentColumns,
                beLongUser,
                beLongGoods,
                beLongCompany,
                validateInfos,
                loginUser
            }
        }
    }
</script>

<style scoped>

</style>